package com.cloudm.technician.ui.modules.cloudm_box.machine

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.MachineInfo
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseListFragment
import kotlinx.android.synthetic.main.fragment_machine_list.*

/**
 * 作者: wesley
 * 时间: 2017/12/20
 * 描述: ui
 */
class MachineFragment : BaseListFragment<MachinePresenter, MachineInfo>(), MachineContract.View {

    var listener: OnTitleReceiveListener? = null
    private var flag = 0
    private var typeId = -1
    private var brandId = -1
    private var modelId = -1

    private lateinit var adapter: MachineAdapter

    override val enabledLoadMore: Boolean
        get() = false

    override val enabledRefresh: Boolean
        get() = false

    companion object {
        private const val FLAG = "FLAG"
        private const val TYPEID = "TYPEID"
        private const val BRANDID = "BRANDID"
        private const val MODELID = "MODELID"
        fun newInstance(flag: Int, typeId: Int, brandId: Int,
                        modelId: Int) = MachineFragment().apply {
            arguments = Bundle().apply {
                putInt(FLAG, flag)
                putInt(TYPEID, typeId)
                putInt(BRANDID, brandId)
                putInt(MODELID, modelId)
            }
        }
    }

    override fun onItemClick(t: MachineInfo) {
        val intent = Intent().apply {
            putExtras(Bundle().apply {
                putParcelable("info", t)
            })
        }
        val activity = mContext as? MachineActivity ?: return
        activity.setResult(Activity.RESULT_OK, intent)
        activity.finish()
    }

    override fun initInject() {
        DaggerMachineComponent.builder().appComponent(appComponent).machinePresenterModule(
                MachinePresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        flag = bundle.getInt(FLAG)
        typeId = bundle.getInt(TYPEID)
        brandId = bundle.getInt(BRANDID)
        modelId = bundle.getInt(MODELID)
    }

    override fun createAdapter(): BaseAdapter<MachineInfo> {
        adapter = MachineAdapter()
        return adapter
    }

    override fun loadData() {
        presenter?.loadData(flag, typeId, brandId, modelId)
    }

    override fun initViews(view: View) {
        super.initViews(view)
        presenter?.buildTitleByFlag(flag)
    }


    override fun onMachineTypeTitleReceive() {
        listener?.onTitleReceive(mContext.resources.getString(R.string.machine_type))
    }

    override fun onMachineBrandTitleReceive() {
        listener?.onTitleReceive(mContext.resources.getString(R.string.machine_brand))
    }

    override fun onMachineModelTitleReceive() {
        listener?.onTitleReceive(mContext.resources.getString(R.string.machine_model))
    }

    override fun scrollToPosition(position: Int) {
        smoothScrollToPosition(position)
    }

    override fun initEmptyResource() {
        super.initEmptyResource()
        viewStub.layoutResource = R.layout.empty_machine_view
    }


    interface OnTitleReceiveListener {
        fun onTitleReceive(title: String)
    }
}